package org.dkakunsi.aplikasitoko.data.util;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

	private static SessionFactory sessionFactory;
	
	static {
		try {
			sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
		} catch(Throwable exc) {
			throw new ExceptionInInitializerError(exc);
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static Session openSession(){
		return sessionFactory.openSession();
//		return getCurrentSession();
	}
	
	public static Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	
	public static void close() {
		if(sessionFactory != null)
			getSessionFactory().close();			
		sessionFactory = null;
	}
	
	public static void close(Session session) {
		if( session != null && session.isOpen() )
			session.close();
	}
}
